<?php

class Flickr extends Plugins implements IPlugins {
	
	public $BASE_URL = "http://api.flickr.com/services/rest/?";
	private $method;
	private $api_key = "a4292b0eb720f723443391495bf0f2f3";
	private $secret = "70987ba9a6acb431";
	public $output = array();
		
	function __construct($query, $max_results) {
		
		/*
		* Add metadata to output array
		*/
		$this->output["feed"] = "Flickr";
		
		$this->method = array(
				"method" => "flickr.photos.search",
				"tags" => $query,
				"api_key" => $this->api_key,
				"per_page" => $max_results
			);
		
	}
	
	function load() {

		/**
		* Format querystring & fetch feed
		*/
		
		$query = parent::query_params($this->method);
		
		// var_dump($this->BASE_URL . $query);
		$xml = parent::load_xml($this->BASE_URL, $query);
		
		$entries = $xml->getElementsByTagName("photo");
		
		$feed = array();
		
		/**
		* Set up items
		*/
		foreach ($entries as $entry)
		{	
			
			$item = array();
			
			$item["network"] = "flickr";
			$item["id"] = $entry->getAttribute("id");
			$item["owner"] = $entry->getAttribute("owner");
			$item["secret"] = $entry->getAttribute("secret");
			$item["server"] = $entry->getAttribute("server");
			$item["farm"] = $entry->getAttribute("farm");
			$item["title"] = $entry->getAttribute("title");
			$item["thumb"] = "http://farm" . $item["farm"] . ".static.flickr.com/" . $item["server"] . "/" . $item["id"] . "_" . $item["secret"] . "_s.jpg";
			
			array_push($feed, $item);
			
			/**
			* Create a "limited" general object to access everything in the feed.
			*/
			parent::create_socialmash_object($item);
		}
			
		$this->output["items"] = $feed;
		
		
	}

	function render()
	{
		foreach ($this->output["items"] as $item)
			echo("<a href='" . $item["thumb"] . "'>" . $item["title"] . "</a><br />");
	}
	
	function toJson()
	{
		parent::toJson($this->output);
	}
	
	function toString() {
		return parent::toString();
	}
}

?>